package com.sprucetec.livechat.processor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.HashMap;

/**
 * @ClassName ChatEnvProcessor
 * @Description: 动态替换application.properties中的参数
 * @Author Kenny
 * @Date 2020/4/24
 **/
@Configuration
@Component
@Slf4j
public class ChatEnvProcessor implements EnvironmentPostProcessor {
    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication springApplication) {
        InetAddress address = null;
        String hostAddress = null;
        try {
            address = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        if (environment.getProperty("os.name").indexOf("Windows") != -1) {
            hostAddress = address.getHostAddress().replaceAll("\\.", "-");//返回IP地址
        } else {
            hostAddress = getLinuxLocalIp().replaceAll("\\.", "-");//返回IP地址
        }
        HashMap<String, Object> map = new HashMap<>(1);
        map.put("server.ip", hostAddress);
        log.info("当前聊天服务器IP是： " + hostAddress);

        PropertySource source = new MapPropertySource("server.ip", map);
        environment.getPropertySources().addFirst(source);
    }

    public static String getLinuxLocalIp() {
        String ip = "";
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
                 en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                String name = intf.getName();
                if (!name.contains("docker") && !name.contains("lo")) {
                    for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
                         enumIpAddr.hasMoreElements(); ) {
                        InetAddress inetAddress = enumIpAddr.nextElement();
                        if (!inetAddress.isLoopbackAddress()) {
                            String ipaddress = inetAddress.getHostAddress();
                            if (!ipaddress.contains("::") && !ipaddress.contains("0:0:")
                                    && !ipaddress.contains("fe80")) {
                                ip = ipaddress;
                            }
                        }
                    }
                }
            }
        } catch (SocketException ex) {
            System.out.println("获取ip地址异常");
            ex.printStackTrace();
        }
        System.out.println("IP:" + ip);
        return ip;
    }

}
